const fs = require('fs');

let completedTasks = 0;
const tasks = [];
const wordCounts = [];
const filesDir = './chapter03/flow/text';

function checkIfComplete() {
  completedTasks++;
  if (completedTasks == tasks.length) {
    for (const index in wordCounts) {
      console.log(`${index}: ${wordCounts[index]}`);
    }
  }
}

function countWordsInText(text) {
  const words = text.toString().toLowerCase().split(/\W+/).sort();
  for (const index in words) {
    const word = words[index];
    wordCounts[word] = wordCounts[word] ? wordCounts[word] + 1 : 1;
  }
}

fs.readdir(filesDir, function (err, files) {
  if (err) {
    throw err;
  }

  for (const index in files) {
    const task = (function (file) {
      return function () {
        fs.readFile(file, function (err, text) {
          if (err) {
            throw err;
          }
          countWordsInText(text);
          checkIfComplete();
        });
      };
    })(`${filesDir}/${files[index]}`);
    tasks.push(task);
  }

  for (const task in tasks) {
    tasks[task]();
  }
});
